package com.novellotus.iov.carvice.adapter;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.bean.RxVehiclesType;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Rich on 2018/1/8.
 *
 */

public class CarScreenItemAdapter extends RecyclerView.Adapter<CarScreenItemAdapter.Item> {

    private int type;//0 车辆状态  1车型
    private List<RxVehiclesType> rxVehiclesTypeList;
    private int clickPosition = 0;
    private CarScreenItemClickInterface carScreenItemClickInterface;
    private boolean isOutGoing;

    CarScreenItemAdapter(int type, int position, List<RxVehiclesType> rxVehiclesTypeList, CarScreenItemClickInterface carScreenItemClickInterface, boolean isOutGoing) {
        this.type = type;
        this.rxVehiclesTypeList = rxVehiclesTypeList;
        this.carScreenItemClickInterface = carScreenItemClickInterface;
        this.isOutGoing = isOutGoing;
        if(isOutGoing){
            clickPosition=0;
        }else{
            clickPosition = position;
        }


    }


    @Override
    public Item onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_screen, null);
        return new Item(view);
    }

    @Override
    public void onBindViewHolder(Item holder, int position) {
        if (type == 0) {
            initCarStatue(holder, position);
        } else {
            initCarModel(holder, position);
        }
        if (position == clickPosition) {
            holder.btn.setTextColor(Color.parseColor("#00a8ff"));
            holder.btn.setBackgroundColor(Color.parseColor("#deeff7"));
        } else {
            holder.btn.setTextColor(Color.parseColor("#333333"));
            holder.btn.setBackgroundColor(Color.parseColor("#f7f7f7"));
        }
    }

    private void initCarStatue(Item holder, final int position) {
        if (isOutGoing) {
            holder.btn.setText("入库(已上报)");
        } else {
            switch (position) {
                case 0:
                    holder.btn.setText("全部");
                    break;
                case 1:
                    holder.btn.setText("入库");
                    break;
                case 2:
                    holder.btn.setText("入库(未上报)");
                    break;
                case 3:
                    holder.btn.setText("入库(已上报)");
                    break;
                case 4:
                    holder.btn.setText("售出");
                    break;
            }
            holder.btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    carScreenItemClickInterface.chooseStatue(position);
                    clickPosition = position;
                    notifyDataSetChanged();
                }
            });
        }

    }

    private void initCarModel(Item holder, final int position) {
        if (position == 0) {
            holder.btn.setText("全部");

        } else {
            holder.btn.setText(rxVehiclesTypeList.get(position - 1).getName());

        }
        holder.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (position == 0) {
                    carScreenItemClickInterface.chooseModel("", position);
                } else {
                    carScreenItemClickInterface.chooseModel(rxVehiclesTypeList.get(position - 1).getId(), position);
                }
                clickPosition = position;
                notifyDataSetChanged();
            }
        });


    }

    @Override
    public int getItemCount() {
        if (type == 0) {
            if (isOutGoing) {
                return 1;
            } else {
                return 5;
            }

        } else {
            return rxVehiclesTypeList.size() + 1;
        }
    }

    class Item extends RecyclerView.ViewHolder {
        @BindView(R.id.btn)
        Button btn;

        Item(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface CarScreenItemClickInterface {
        void chooseStatue(int position);

        void chooseModel(String id, int position);
    }
}
